package com.hmys.api.controller;

import com.github.pagehelper.PageInfo;
import com.hmys.api.model.WebCounselor;
import com.hmys.api.model.WebCounselorItem;
import com.hmys.api.model.WebCounselorWithBLOBs;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.response.ResultPage;
import com.hmys.api.service.WebCounselorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/03/26.
 */
@RestController
@Api(tags = "顾问")
@RequestMapping(value = "/counselor")
public class CounselorController {

    @Autowired
    private WebCounselorService webCounselorService;

    @ApiOperation(value = "列表(简易)", notes = "列表(简易)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageIndex", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Boolean", name = "recommend", value = "是否推荐", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "locationId", value = "工作地点ID", required = false, paramType = "query")
    })
    @GetMapping(value = "/list/item/{pageIndex}/{pageSize}")
    public Object item(@PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize, Boolean recommend, Long locationId) {
        WebCounselor record = new WebCounselor();
        if (!StringUtils.isEmpty(recommend)) {
            record.setIsRecommend(recommend);
        }
        if (!StringUtils.isEmpty(locationId)) {
            record.setLocationId(locationId);
        }
        record.setStatus(true);
        PageInfo<WebCounselorItem> pageInfo = webCounselorService.selectItemByBean(pageIndex, pageSize, record);
        if (pageInfo.getList().size() > 0) {
            return new ResultPage(ResultCode.SUCCESS, pageInfo);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "列表(详情)", notes = "列表(详情)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageIndex", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Boolean", name = "recommend", value = "是否推荐", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Long", name = "locationId", value = "工作地点ID", required = false, paramType = "query")
    })
    @GetMapping(value = "/list/{pageIndex}/{pageSize}")
    public Object list(@PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize, Boolean recommend, Long locationId) {
        WebCounselor record = new WebCounselor();
        if (!StringUtils.isEmpty(recommend)) {
            record.setIsRecommend(recommend);
        }
        if (!StringUtils.isEmpty(locationId)) {
            record.setLocationId(locationId);
        }
        record.setStatus(true);
        PageInfo<WebCounselorWithBLOBs> pageInfo = webCounselorService.selectByBean(pageIndex, pageSize, record);
        if (pageInfo.getList().size() > 0) {
            return new ResultPage(ResultCode.SUCCESS, pageInfo);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "详情", notes = "详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "id", value = "新闻ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/detail/{id}")
    public Object detail(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebCounselorWithBLOBs record = webCounselorService.selectByPrimaryKey(id);
        if (record != null) {
            if (record.getStatus()) {
                return new Result<>(ResultCode.SUCCESS, record);
            } else {
                return new Result<>(ResultCode.HAVE_DELETED);
            }
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }
}
